<?php

declare(strict_types=1);

namespace app\admin\logic;

use app\common\logic\Banner as LogicBanner;
use app\common\model\Banner as ModelBanner;

class Banner extends LogicBanner
{
    public static function getDataTable(int $page, int $limit)
    {
        $query = ModelBanner::order('sort', 'desc');
        $count = $query->count();
        $list = $query->select()->hidden(['create_time', 'delete_time'])->toArray();
        return compact('count', 'list');
    }

    /**
     * 获取列表
     */
    public static function getAdminLists()
    {
        $data = parent::getLists();
        return $data->hidden(['uniacid', 'create_time', 'delete_time'])->toArray();
    }

    /**
     * 添加
     *
     * @param array $data
     */
    public static function save(array $data)
    {
        try {
            ModelBanner::create($data);
        } catch (\think\db\exception\PDOException $e) {
            fault('添加失败');
        }
    }

    /**
     * 删除
     *
     * @param int|array $id
     */
    public static function delete($id)
    {
        if (is_array($id)) {
            $model = ModelBanner::whereIn('id', $id)->findOrEmpty();
        } else {
            $model = ModelBanner::findOrEmpty($id);
        }
        $model->isEmpty() && fault('数据不存在');
        $model->delete();
    }

    /**
     * 编辑
     *
     * @param integer $id
     */
    public static function read(int $id): array
    {
        $model = ModelBanner::findOrEmpty($id);
        $model->isEmpty() && fault('数据不存在');
        return $model->toArray();
    }

    /**
     * 更新
     *
     * @param integer $id
     * @param array   $data
     */
    public static function update(int $id, array $data): void
    {
        $model = ModelBanner::findOrEmpty($id);
        $model->isEmpty() && fault('数据不存在');
        $model->save($data);
    }
}
